package com.terren.spider.core.html.scheme.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.terren.spider.core.html.configuration.Configuration;
import com.terren.spider.core.html.scheme.impl.BasicCustomScheme;
import com.terren.spider.db.service.AppDataService;
import com.terren.spider.entity.biz.AppRequestParam;
import com.terren.spider.entity.core.Entry;
import com.terren.spider.util.common.CommonUtil;

public class DouyinScheme extends BasicCustomScheme{
	
	private static AppDataService appDataService;
	
	@Override
	public Set<String> addEntryUrl(Configuration configuration) {
		Set<String> idSet=new HashSet<>();
		appDataService=CommonUtil.getBean(AppDataService.class);
		AppRequestParam param=new AppRequestParam();
		param.setAppId(1000);
		try {
			List<AppRequestParam> paramList=appDataService.getAppRequestParam(param);
			idSet.add(paramList.get(0).getRequestUrl());
			configuration.getEntry().setKeyword(paramList.get(0).getKeyword());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return idSet;
	}
	
	@Override
	public Map<String, String> getHeader(Entry entry) {
		Map<String, String> map=new HashMap<>();
		appDataService=CommonUtil.getBean(AppDataService.class);
		AppRequestParam param=new AppRequestParam();
		param.setAppId(1000);
		try {
			List<AppRequestParam> paramList=appDataService.getAppRequestParam(param);
			JSONObject jsonObj=JSON.parseObject(paramList.get(0).getRequestHeader());
			Set<String> set=jsonObj.keySet();
			List<String> list=new ArrayList<>(set);
			
			for (int i = 0; i < list.size(); i++) {
				map.put(list.get(i), StringUtils.strip(jsonObj.get(list.get(i)).toString(),"[]").replaceAll("\"",""));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
}

